//package com.candy.databasedocuments.config;
//
//import org.apache.catalina.connector.Connector;
//import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
//import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
//import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
//import org.springframework.context.ApplicationEvent;
//import org.springframework.context.ApplicationListener;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//
//import java.util.concurrent.Executor;
//import java.util.concurrent.ThreadPoolExecutor;
//import java.util.concurrent.TimeUnit;
//
///**
// * @author suYanLong
// * @version 1.0
// * @date 2023/7/11 16:48
// * @Description 优雅停机实现
// * kill -9 pid # 暴力删除
// * kill -15 pid #比较优雅关闭项目，sleep线程会有问题，线程调用interrupt
// * ConfigurableApplicationContext.close(); #比较优雅关闭项目，sleep线程会有问题，线程调用interrupt
// * spring-boot-starter-actuator 停止线程池的时候还是调用了线程的interrupt方法，导致sleep报错
// * 本文停机方式相对更优雅
// * 数据备份操作@PreDestroy：在容器停止之前执行一次，你可以在这里面做备份操作，也可以做记录停机时间等
// */
//@Configuration
//public class ShutdownConfig implements TomcatConnectorCustomizer, ApplicationListener {
//
//    private volatile Connector connector;
//
//    private final int waitTime = 10;
//
//    @Bean
//    public ShutdownConfig elegantShutdownConfig() {
//        return new ShutdownConfig();
//    }
//
//    @Bean
//    public ServletWebServerFactory servletContainer() {
//        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
//        tomcat.addConnectorCustomizers(elegantShutdownConfig());
//        return tomcat;
//    }
//
//    @Override
//    public void customize(Connector connector) {
//        this.connector = connector;
//    }
//
//    @Override
//    public void onApplicationEvent(ApplicationEvent event) {
//        connector.pause();
//        Executor executor = connector.getProtocolHandler().getExecutor();
//        if (executor instanceof ThreadPoolExecutor) {
//            try {
//                ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor;
//                threadPoolExecutor.shutdown();
//                if (!threadPoolExecutor.awaitTermination(waitTime, TimeUnit.SECONDS)) {
//                    System.out.println("请尝试暴力关闭");
//                }
//            } catch (InterruptedException ex) {
//                System.out.println("异常了");
//                Thread.currentThread().interrupt();
//            }
//        }
//    }
//
//}
